<?php
namespace app\api\validate;
use think\Validate;

class video extends Validate
{
    protected $rule = [
        'id'            =>  'require|number',
        'brand_id'      =>  'require|number',
        'product_id'    =>  'require|number',
        'video'         =>  'require|checkVideo',
        'advertiser_id' =>  'require',
        'video_info'         =>  'require|array',
        'advertiser_info'    =>  'require|array',
    ];

    protected $message = [
        'id.require'            => '视频ID不能为空!',
        'id.number'             => '视频ID参数错误!',
        'brand_id.require'      => '品牌ID不能为空!',
        'brand_id.number'       => '品牌ID参数错误!',
        'product_id.require'    => '产品ID不能为空!',
        'product_id.number'     => '产品ID参数错误!',
        'video.require'         => '视频信息不能为空!',
        'advertiser_id.require' => '广告主ID不能为空!',
        'video_info.require'        => '视频信息不能为空!',
        'video_info.array'          => '视频信息参数类型错误!',
        'advertiser_info.require'   => '广告主信息不能为空!',
        'advertiser_info.array'     => '广告主信息参数类型错误!',
    ];

    protected $scene = [
        'log'         =>  ['id'],
        'check_again' =>  ['id'],
        'delete'      =>  ['id'],
        'upload'      =>  ['brand_id','product_id','video'],
        'advertiser'  =>  ['advertiser_id'],
        'push'        =>  ['video_info','advertiser_info'],
    ];
    
    protected function checkVideo($value,$rule,$data)
    {
        if(!is_array($value)){
            return '视频信息参数格式错误!';
        }
        foreach ($value as $k => $v) {
            if(!is_array($v)){
                return '视频信息参数格式错误!';
            }
            if(!isset($v['name'])){
                return '视频名称参数不能为空!';
            }
            if(!isset($v['url'])){
                return '视频链接参数不能为空!';
            }
            if(!isset($v['poster'])){
                return '视频封面参数不能为空!';
            }
        }
        return true;
    }
}